/** @odoo-module **/

function commandManager(){
    this.undoList=[];
    this.redoList=[];
    this.undoCount=20; //-1为无限
    this.changed=null;

    let that=this;

    this.reset = function () {
        that.undoList=[];
        that.redoList=[];
        that.undoCount=20; //-1为无限
        if(that.changed!=null){
            that.changed();
        }
    }

    this.executeCommand=function(cmd){
        cmd.execute();
        that.undoList.unshift(cmd);
        if (that.undoCount != -1 && that.undoList.length> that.undoCount) {
            that.undoList.splice(that.undoList.length-1,1);
        }
        that.redoList=[];

        if(that.changed!=null){
            that.changed();
        }
    }
    this.undo=function(){
        if (that.undoList.length <= 0) {
            return;
        }
        let cmd = that.undoList[0];
        cmd.undo();

        that.undoList.splice(0,1);
        that.redoList.unshift(cmd);

        if(that.changed!=null){
            that.changed();
        }
    }
    this.redo=function(){
        if (that.redoList.length <= 0) {
            return;
        }
        let cmd = that.redoList[0];
        cmd.redo();

        that.redoList.splice(0,1);
        that.undoList.unshift(cmd);

        if(that.changed!=null){
            that.changed();
        }
    }
    this.canUndo=function(){
        return that.undoList.length>0;
    }
    this.canRedo=function(){
        return that.redoList.length>0;
    }
};

function baseCommand(){
    let options=null;
    this.setOptions=function(newOptions){
        options=newOptions;
    }
    this.execute=function(){
        console.log("未指定执行方法.");
    }
    this.undo=function(){
        console.log("未指定撤销方法.");
    }
    this.redo=function(){
        console.log("未指定重做方法.");
    }
}
export {
    commandManager,baseCommand
}
